package com.hmyd.hmyd_android.ui;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.AppBasic;
import com.hmyd.hmyd_android.commons.UrlConstant;
import com.hmyd.hmyd_android.crypto.Crypto;
import com.hmyd.hmyd_android.utils.MyToast;
import com.hmyd.hmyd_android.utils.PreferencesUtils;
import com.hmyd.hmyd_android.utils.Utils;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

/**
 * 修改密码
 * 
 * @author wangc
 * 
 */
@ContentView(R.layout.activity_password_update)
public class ChangePasswordActivity extends BasicActivity implements OnClickListener {
	// 标题栏
	@ViewInject(R.id.home_title)
	public View home_title;
	@ViewInject(R.id.home_title_txt)
	public TextView home_title_txt;
	@ViewInject(R.id.home_right)
	public TextView home_right;

	@ViewInject(R.id.old_password)
	private EditText old_password;
	@ViewInject(R.id.new_password)
	private EditText new_password;
	@ViewInject(R.id.re_password)
	private EditText re_password;

	@ViewInject(R.id.save)
	private Button save;

	private final int SUBMIT = 1;

	private TextWatcher watcher = new TextWatcher() {

		@Override
		public void afterTextChanged(Editable s) {
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			if (TextUtils.isEmpty(old_password.getText().toString())
					|| TextUtils.isEmpty(new_password.getText().toString())
					|| TextUtils.isEmpty(re_password.getText().toString())) {
				save.setEnabled(false);
			} else {
				save.setEnabled(true);
			}
		}
	};

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.save:
			if (Utils.regPatternsPassword(this, new_password.getText().toString().trim())
					&& Utils.regPatternsPassword(this, re_password.getText().toString().trim())) {// 密码验证不通过

			} else if (!new_password.getText().toString().trim()
					.equals(re_password.getText().toString().trim())) {
				MyToast.showToast(this, "两次密码输入不一致");
			} else {
				RequestParams params = new RequestParams();
				params.addBodyParameter("oldpassword", old_password.getText().toString().trim());
				params.addBodyParameter("newpassword", new_password.getText().toString().trim());

				onRequest(SUBMIT, UrlConstant.CHANGEPASSWORD, HttpMethod.POST, params);
			}
			break;
		default:
			break;
		}
	}

	public void onPostSuccess(int postId, String result) {
		switch (postId) {
		case SUBMIT:
			MyToast.showHMToast(this, true, "密码修改成功");

			PreferencesUtils.putString(this, PreferencesUtils.USER_PASSWORD,
					Crypto.encryptAES(new_password.getText().toString(), Crypto.PRIVATEKEY));

			finish();
			break;

		default:
			break;
		}
	}

	@Override
	protected void setParams() {
		home_title_txt.setText(getString(R.string.setting_password_modify));
		home_title.setOnTouchListener(touch);

		old_password.addTextChangedListener(watcher);
		new_password.addTextChangedListener(watcher);
		re_password.addTextChangedListener(watcher);

		save.setOnClickListener(this);
	}

	@Override
	protected void leftButton() {
		AppBasic.getInstance().finishCurrentActivity();
	}

	@Override
	protected void rightButton() {

	}

	@Override
	public void onPostFailure(int postId, String msg) {

	}

}
